<div class="wrapper clearfix">

    <?php if(!$books): ?>

    	<div class="alert alert-info" style="margin-top:20px; text-align:center;">书本信息不存在</div>

    <?php endif; ?>

    <?php foreach ($books as $bookVal): ?>

    <div class="contenter f-r">
    	
            <div class="shopimg">
            	
            	<img src="<?php echo base_url("uploads/thumb_350/".$bookVal->book_img); ?>" />

            </div><!-- shopimg f-l -->

            <div class="name">
        			<h1><?php echo $bookVal->book_name; ?></h1>
        			<strong><?php echo $bookVal->book_summary; ?></strong>
        	</div><!-- name -->
    
    </div><!-- contenter -->
    <?php endforeach; ?>

</div><!-- wrapper -->

<div class="wrapper">
	
	<div class="shopinfo">
		
		<div class="value"> 
			
            <ol class="fz clearfix"><dt>作业本价：</dt><dl>￥<?php echo $bookVal->book_price; ?></dl></ol>			
			<ol class="market clearfix"><dt>市&nbsp;&nbsp;场&nbsp;&nbsp;价：</dt><dl>￥<?php echo $bookVal->book_pricing; ?></dl></ol>

		</div><!-- value -->

        <div class="value"> 
                	<ol class="delivery clearfix"><dt>品&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;牌：</dt><dl><?php echo $bookVal->pub_name; ?></dl></ol>			
                    			
                    <ol class="delivery clearfix"><dt>商品编号：</dt><dl><?php echo $bookVal->book_isbn?$bookVal->book_isbn:"无"; ?></dl></ol>			
                    <ol class="delivery clearfix"><dt>月&nbsp;&nbsp;销&nbsp;&nbsp;量：</dt><dl><?php echo $bookVal->book_buy+$bookVal->book_vbuy; ?></dl></ol>
                    <ol class="delivery clearfix"><dt>库&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;存：</dt><dl><?php echo $bookVal->book_store?$bookVal->book_store:'<span class="label label-danger" style="font-weight:normal">缺货</span>'; ?></dl></ol>
					<ol class="delivery clearfix"><dt>享受服务：</dt><dl style="color:#FF4500">作业本送货上门，南林创业园101室，7天包退换！</dl></ol>
					
        		</div><!-- value -->

        <div class="shopcar">
        	
            <button class="btn btn-default l">-</button>
            <input type="text" class="form-control" value="1" maxlength="3" />
            <button  class="btn btn-default r">+</button>
            <input type="button" data-bookid="<?php echo $bookVal->book_id; ?>" class="btn btn-primary addshopcar" value="加入购物车" />
            <!--<input type="button" data-bookid="<?php //echo $bookVal->book_id; ?>" class="btn btn-default <?php //if($this->m_bookmark->selectBookmark($bookVal->book_id,$this->session->userdata('user_id'))){echo 'btn-success';}?> collect" value="收藏" />-->
        </div><!-- shopcar -->
  
        </div><!-- shopinfo -->
</div>


<?php $this->load->view("lists/shopcarModal");?>

<script>

// decrease , button click
$(".shopcar").children('.l').click(function(){
    $($(this).parent().children('.form-control')).popover('destroy');
    $(this).parent().removeClass('has-error');

    // decrease button click
    if(parseInt($(this).parent().children('.form-control').val()) <= 1)
    {
        // when input val < 1 ,show error
        $(this).parent().children('.form-control').parent().addClass('has-error');
        $(this).parent().children('.form-control').attr('data-content','本商品一件起卖');
        $(this).parent().children('.form-control').attr('data-placement','top');
        $(this).parent().children('.form-control').popover('show');
        $(this).parent().children('.form-control').val('1'); //set input val = 1
    }
    else
    {
        $(this).parent().children('.form-control').val(parseInt($(this).parent().children('.form-control').val()) - 1);
    }

});

// increase, click button
$(".shopcar").children('.r').click(function(){

    // clear error
    $($(this).parent().children('.form-control')).popover('destroy');
    $(this).parent().removeClass('has-error');

    $(this).parent().children('.form-control').val(parseInt($(this).parent().children('.form-control').val()) + 1);

});

//clear error
$("input").focus(function(){
        
    $(this).popover('destroy');
    $(this).parent().removeClass('has-error');
});

//当用户输入的商品数量小于1,则提示用户，并强行设置数量为1
$(".shopcar").children('.form-control').change(function(){
    if( (parseInt($(this).val()) <= 1) || ($(this).val() ==''))
    {
        // when input val < 1 ,show error
        $(this).parent().addClass('has-error');
        $(this).attr('data-content','本商品一件起卖');
        $(this).attr('data-placement','top');
        $(this).popover('show');
        $(this).val('1'); //set input val = 1 
    }

});

// 将物品加入购物车
$(".addshopcar").click(function(){

    //clear error
    $($(this).parent().children('.form-control')).popover('destroy');
    $(this).parent().removeClass('has-error');

    var qty = $(this).prevAll('.form-control').val(); // 用户购买的物品数量
    var id = $(this).attr('data-bookid'); // 用户购买物品的ID
    var bookname = $(".shopinfo .name").children("h1").html(); //商品的名称
    
    $("#shopcarModal .book-info h3 a").html(bookname); 
    
    var this_form = $(this).siblings('.form-control');
    
    $.ajax({
    	
        url:"<?php echo base_url('shopcar/addShopCarAjax');?>",
        dataType:'json',
        type:'post',
        data:{"qty":qty,"id":id},
        success:function(e){
            if(e.store_warn){
                this_form.parent().addClass('has-error');
                this_form.attr('data-content','库存不足！%>_<%');
                this_form.attr('data-placement','top');
                this_form.popover('show');
            }else{
            	$("#shopcarModal").modal("show");
            	$("#showcontent").html(e);
            	
            	$("#shopcarAmount").html(e.amount);
            	$("#shopcarPirce").html("￥ "+e.price);

            	//改变头部购物车商品数量
            	$("#topShopcar em").html(e.amount);
            }  
        }
	});
});

function clearError(id){
    $(id).popover('destroy');
    $(id).parent().removeClass('has-error');
}
</script>
<script src="<?php echo base_url('static/javascript/lists/collect.js');?>"></script>